package view;

import dao.BookDao;
import service.BookService;
import service.UserService;

import java.io.*;
import java.util.Scanner;

/**
 * @Description 主页面
 * @Author hhy
 */
public class View {
    Scanner sc = new Scanner(System.in);
    UserService userService = new UserService();
    BookService bookService = new BookService();
    BookDao bookDao = new BookDao();


    public void init() throws IOException {
        System.out.println("******欢迎来到图书管理系统******");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出系统");
        int num = 0;
        if (sc.hasNextInt()) {
            num = sc.nextInt();
        }else {
            System.out.println("你不对劲");
        }
            switch (num) {
                case 1: //登录功能
                    if (userService.userLogin()) {
                        System.out.println("欢迎登陆！"+userService.getUserName()+"!");
                        menu();
                    } else {
                        System.out.println("登录失败");
                        init();
                    }
                    break;
                case 2: //注册功能
                    if (userService.userRegister()) {
                        System.out.println("注册成功");
                        init();
                    } else {
                        System.out.println("注册失败");
                        init();
                    }
                    break;
                case 666: //查看所有用户（测试使用
                    userService.selectAlluser();
                    init();
                    break;
                default:
                    System.out.println("您已成功退出系统，欢迎下次再来！");
                    break;
            }
        }


    public void menu() throws IOException {
        System.out.println("欢迎来到图书菜单，请选择您需要的服务");
        System.out.println("1.查看书籍列表");
        System.out.println("2.借书");
        System.out.println("3.还书");
        System.out.println("4.返回上一级");
        int num = 0;
        if (sc.hasNextInt()) {
            num = sc.nextInt();
        }else {
            System.out.println("你不对劲");
        }
            switch (num) {
                case 1:
                    bookDao.lookAllBooks();
                    menu();
                    break;
                case 2:
                    System.out.println("请选择你要借的书");
                    bookService.lookAllBooks();
                    int bookId = 0;
                    if (sc.hasNextInt()) {
                        bookId = sc.nextInt();
                    } else {
                        System.out.println("请输入图书的序号");
                        menu();
                        break;
                    }
                    if (bookService.JS(1, bookId)) {
                        System.out.println("已成功借书！！！");

                    } else {
                        System.out.println("借书失败！！！");
                    }
                    menu();
                    break;
                case 3:
                    System.out.println("请选择你要还的书");
                    bookService.lookAllBooks();
                    int bookId2 = 0;
                    if (sc.hasNextInt()) {
                        bookId2 = sc.nextInt();
                    } else {
                        System.out.println("请输入图书的序号");
                        menu();
                        break;
                    }
                    if (bookService.JS(0, bookId2)) {
                        System.out.println("已成功还书");
                    } else {
                        System.out.println("还书失败！！！");
                    }
                    menu();
                    break;
                default:
                    init();
                    break;
            }

    }



}
